package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.PermissionDao;
import com.itheima.dao.RoleDao;
import com.itheima.dao.UserDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Permission;
import com.itheima.pojo.Role;
import com.itheima.pojo.User;
import com.itheima.service.UserService;
import com.itheima.utils.MD5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

@Service(interfaceClass = UserService.class)
@Transactional
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao userDao;
    @Autowired
    private RoleDao roleDao;
    @Autowired
    private PermissionDao permissionDao;

    @Override
    public User findUserByUserName(String username) {
        //查询用户信息
        User user = userDao.findUserByUserName(username);
        if (user == null) {
            return null;
        }
        //通过用户id查询角色信息
        Set<Role> roleList = roleDao.findRoleByUserId(user.getId());
        if (roleList != null && roleList.size() > 0) {
            for (Role role : roleList) {
                //通过角色id查询权限信息
                Set<Permission> permissionList = permissionDao.findPermissionByRoleId(role.getId());
                if (permissionList != null && permissionList.size() > 0) {
                    role.setPermissions(permissionList);
                }
                user.setRoles(roleList);
            }
        }
        return user;
    }
    //分页查询用户信息
    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        String queryString = queryPageBean.getQueryString();
        Integer pageSize = queryPageBean.getPageSize();
        Integer currentPage = queryPageBean.getCurrentPage();
        PageHelper.startPage(currentPage,pageSize);
        Page<User> page = userDao.findPage(queryString);
        long total = page.getTotal();
        List<User> result = page.getResult();
        return new PageResult(total,result);
    }
    //添加用户
    @Override
    public void add(User user, Integer[] roleIds) {
        userDao.add(user);
        Integer userId = user.getId();
        if (roleIds!=null&&roleIds.length>0){
            for (Integer roleId : roleIds) {
                Map<String,Integer> map=new HashMap<>();
                map.put("userId",userId);
                map.put("roleId",roleId);
                userDao.setUserAndRole(map);
            }

        }
    }
    //根据id查询用户信息
    @Override
    public User findById(Integer userId) {
        return  userDao.findById(userId);
    }

    @Override
    public List<Integer> findroleIdsByuserId(Integer userId) {

        return userDao.findroleIdsByuserId(userId);
    }
    //编辑用户信息
    @Override
    public void edit(User user, Integer[] roleIds) {
        userDao.edit(user);
        userDao.deleteAssocication(user.getId());
        Integer userId = user.getId();
        if (roleIds!=null&&roleIds.length>0){
            for (Integer roleId : roleIds) {
                Map<String,Integer> map=new HashMap<>();
                map.put("userId",userId);
                map.put("roleId",roleId);
                userDao.editByUserAndRole(map);
            }
        }
    }
    //删除用户
    @Override
    public void delete(Integer id) {
        List<Integer> list = findroleIdsByuserId(id);
        if (list.size()>0&&list!=null){
            throw  new RuntimeException("有关联关系，删除失败");
        }else {
            userDao.delete(id);
        }
    }

    @Override
    public void editPassword(User user, String validateCode) {
        userDao.editPassword(user);
    }


}
